package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.GameInfoDTO;
import top.zywork.query.GameInfoQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GameInfoService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.GameInfoVO;

import java.util.List;

/**
 * GameInfoController控制器类<br/>
 *
 * 创建于2020-05-13<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/game-info")
@Slf4j
public class GameInfoController extends BaseController {

    private GameInfoService gameInfoService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存GameInfoVO")
    public ResponseStatusVO save(@RequestBody @Validated GameInfoVO gameInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        gameInfoService.save(BeanUtils.copy(gameInfoVO, GameInfoDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存GameInfoVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<GameInfoVO> gameInfoVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        gameInfoService.saveBatch(BeanUtils.copyListObj(gameInfoVOList, GameInfoDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除GameInfoVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        gameInfoService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除GameInfoVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        gameInfoService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新GameInfoVO")
    public ResponseStatusVO update(@RequestBody @Validated GameInfoVO gameInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = gameInfoService.update(BeanUtils.copy(gameInfoVO, GameInfoDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新GameInfoVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<GameInfoVO> gameInfoVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        gameInfoService.updateBatch(BeanUtils.copyListObj(gameInfoVOList, GameInfoDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结GameInfoVO")
    public ResponseStatusVO active(@RequestBody GameInfoVO gameInfoVO) {
        gameInfoService.update(BeanUtils.copy(gameInfoVO, GameInfoDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结GameInfoVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<GameInfoVO> gameInfoVOList) {
        gameInfoService.updateBatch(BeanUtils.copyListObj(gameInfoVOList, GameInfoDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        GameInfoVO gameInfoVO = new GameInfoVO();
        Object obj = gameInfoService.getById(id);
        if (obj != null) {
            gameInfoVO = BeanUtils.copy(obj, GameInfoVO.class);
        }
        return ResponseStatusVO.ok("查询成功", gameInfoVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = gameInfoService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GameInfoVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody GameInfoQuery gameInfoQuery) {
        PagerDTO pagerDTO = gameInfoService.listAllByCondition(gameInfoQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GameInfoVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody GameInfoQuery gameInfoQuery) {
        PagerDTO pagerDTO = gameInfoService.listPageByCondition(gameInfoQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GameInfoVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 获取当前用户的游戏信息
     * @return
     */
    @GetMapping("user/my-info")
    public ResponseStatusVO userGetMyInfo() {
        Long userId = SecurityUtils.getJwtUser().getUserId();
        return getInfoByUserId(userId);
    }

    @GetMapping("any/get-info/{userId}")
    public ResponseStatusVO getInfoByUserId(@PathVariable("userId") Long userId) {
        GameInfoQuery gameInfoQuery = GameInfoQuery.builder().userId(userId).build();
        List<Object> objectList = gameInfoService.listAllByCondition(gameInfoQuery).getRows();
        GameInfoVO gameInfoVO = new GameInfoVO();
        if (objectList.isEmpty()) {
            return ResponseStatusVO.ok("未设置游戏信息", gameInfoVO);
        }
        List<GameInfoVO> gameInfoVOList = BeanUtils.copy(objectList, GameInfoVO.class);
        gameInfoVO = gameInfoVOList.get(0);
        return ResponseStatusVO.ok("查询成功", gameInfoVO);
    }

    @PostMapping("user/save")
    public ResponseStatusVO userSave(@RequestBody @Validated GameInfoVO gameInfoVO, BindingResult bindingResult) {
        gameInfoVO.setUserId(SecurityUtils.getJwtUser().getUserId());
        return save(gameInfoVO, bindingResult);
    }

    @PostMapping("user/update")
    public ResponseStatusVO userUpdate(@RequestBody @Validated GameInfoVO gameInfoVO, BindingResult bindingResult) {
        return update(gameInfoVO, bindingResult);
    }

    @Autowired
    public void setGameInfoService(GameInfoService gameInfoService) {
        this.gameInfoService = gameInfoService;
    }
}